package api

import (
	"gitee.com/DengAnbang/Computation/src/code"
	"gitee.com/DengAnbang/Computation/src/dbops"
	"gitee.com/DengAnbang/Computation/src/bean"
	"net/http"
	"gitee.com/DengAnbang/Computation/src/util"
	"io/ioutil"
	"os"
	"fmt"
	"gitee.com/DengAnbang/goutils/utils"
	"strings"
	"path"
)

/**
	* showdoc
	* @catalog socket文档/模型相关
	* @title 上传b3d模型
	* @description 上传b3d模型的接口
	* @method POST
	* @url /pc/model/file/upload
	* @param nodeId  必选 string 树节点的ID
	* @param userId  必选 string 用户的ID
	* @return {"code":0,"type":0,"message":"","debug_message":"","data":"成功"}
	* @remark 最后修改时间:2019.01.11 10.30
	* @number 1
	*/
func B3DFileUpload(w http.ResponseWriter, r *http.Request) error {
	if r.Method == "GET" {
		fmt.Fprint(w, `<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Upload</title>
</head>
<body>
<form method="POST" action="/pc/model/file/upload" enctype="multipart/form-data">
    Choose an image to upload: <input name="file" type="file" />
 	<input type="text" value="10013" name="userId">
 	<input type="text" value="asdasdasd" name="nodeId">
    <input type="submit" value="上传" />
</form>
</body>
</html>`)
		return nil
	} else if r.Method == "POST" {
		userId := util.GetValueFormRequest(r, "userId")
		nodeId := util.GetValueFormRequest(r, "nodeId")
		if len(userId) <= 0 {
			return bean.NewErrorMessage("用户ID不能为空")
		}
		if len(nodeId) <= 0 {
			return bean.NewErrorMessage("节点ID不能为空")
		}
		file, h, err := r.FormFile("file")
		if err != nil {
			return err
		}
		bytes, err := ioutil.ReadAll(file)
		if err != nil {
			return err
		}
		fileUuid := utils.NewUUID()
		relativePath := "B3D" + "/" + userId + "/" + fileUuid + "/"
		fileParent := code.FileRootPath + relativePath
		os.MkdirAll(fileParent, 0x666)
		resName := fileParent + h.Filename
		err = ioutil.WriteFile(resName, bytes, 0x666)
		if err != nil {
			return err
		}
		err = util.ExtractDb(resName)
		if err != nil {
			return err
		}
		err = dbops.B3DFileUpload(fileUuid, nodeId, h.Filename, relativePath+h.Filename, userId)
		if err != nil {
			return err
		}
		return bean.NewSucceedMessage("成功")
	} else {
		return bean.NewErrorMessage("只支持POST的上传方式!")
	}
	return nil
}
func GetModelBeanListHttp(w http.ResponseWriter, r *http.Request) error {
	nodeId := util.GetValueFormRequest(r, "nodeId")
	beans, err := dbops.SelectModelBean(nodeId)
	if err != nil {
		return err
	}

	return bean.NewSucceedMessage(beans)
}

func GetModelBeanHttp(w http.ResponseWriter, r *http.Request) error {
	nodeId := util.GetValueFormRequest(r, "nodeId")
	beans, err := dbops.SelectModelBean(nodeId)
	if err != nil {
		return err
	}
	if len(beans) > 0 {
		return bean.NewSucceedMessage(beans[0])
	}
	return bean.NewSucceedMessage(nil)
}

func SelectModelTreeHttp(w http.ResponseWriter, r *http.Request) error {
	filePath := util.GetValueFormRequest(r, "filePath")
	if len(filePath) <= 0 {
		return bean.NewErrorMessage("路径不能为空").SetDeBugMessage(filePath)
	}
	fileSuffix := path.Ext(filePath)
	filenameOnly := strings.TrimSuffix(filePath, fileSuffix)
	trees, err := dbops.SelectModelTree(code.FileRootPath + filenameOnly + ".db")
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(trees)
}

func SelectModelNodeAdditionHttp(w http.ResponseWriter, r *http.Request) error {
	filePath := util.GetValueFormRequest(r, "filePath")
	id := util.GetValueFormRequest(r, "id")
	if len(filePath) <= 0 {
		return bean.NewErrorMessage("路径不能为空").SetDeBugMessage(filePath)
	}
	fileSuffix := path.Ext(filePath)
	filenameOnly := strings.TrimSuffix(filePath, fileSuffix)
	additions, err := dbops.SelectModelNodeAddition(code.FileRootPath+filenameOnly+".db", id)
	if err != nil {
		return err
	}
	return bean.NewSucceedMessage(additions)
}
